package com.ld.control.business.robot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.robot.mapper.HuiLingRobotPointMapper;
import com.ld.control.business.robot.model.HuiLingRobotPointModel;
import com.ld.control.business.robot.model.HuiLingRobotPointVariableModel;
import com.ld.control.business.robot.service.HuiLingRobotPointService;
import com.ld.control.business.robot.vo.req.HuiLingRobotPointReqVo;
import com.ld.control.business.robot.vo.resp.HuiLingRobotPointRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 慧灵机械臂点位信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-10-20
 */
@Service
public class HuiLingRobotPointServiceImpl extends ServiceImpl<HuiLingRobotPointMapper, HuiLingRobotPointModel> implements HuiLingRobotPointService {

    @Autowired
    private HuiLingRobotPointMapper huiLingRobotPointMapper;

    @Override
    public void add(HuiLingRobotPointReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        HuiLingRobotPointModel huiLingRobotPointModel = new HuiLingRobotPointModel();
        BeanUtils.copyProperties(reqVo, huiLingRobotPointModel);
        addOrUpdateCover(reqVo, huiLingRobotPointModel);
        huiLingRobotPointMapper.insert(huiLingRobotPointModel);
    }

    @Override
    public void update(HuiLingRobotPointReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        HuiLingRobotPointModel huiLingRobotPointModel = new HuiLingRobotPointModel();
        BeanUtils.copyProperties(reqVo, huiLingRobotPointModel);
        addOrUpdateCover(reqVo, huiLingRobotPointModel);
        huiLingRobotPointMapper.updateById(huiLingRobotPointModel);
    }

    @Override
    public void deleteById(Long id) {
        huiLingRobotPointMapper.deleteById(id);
    }

    @Override
    public HuiLingRobotPointRespVo getById(Long id) {
        HuiLingRobotPointRespVo huiLingRobotPointRespVo = new HuiLingRobotPointRespVo();
        HuiLingRobotPointModel huiLingRobotPointModel = huiLingRobotPointMapper.selectById(id);
        BeanUtils.copyProperties(huiLingRobotPointModel, huiLingRobotPointRespVo);
        String targetPoint = huiLingRobotPointRespVo.getTargetPoint();
        String initPoint = huiLingRobotPointRespVo.getInitPoint();
        if (!ObjectUtils.isEmpty(targetPoint)) {
            huiLingRobotPointRespVo.setTargetPointVariableNameList(JSONObject.parseArray(targetPoint, String.class));
        }
        if (!ObjectUtils.isEmpty(initPoint)) {
            huiLingRobotPointRespVo.setInitPointVariableNameList(JSONObject.parseArray(initPoint, String.class));
        }
        return huiLingRobotPointRespVo;
    }

    @Override
    public GeneratePageListVo<HuiLingRobotPointRespVo> getPageList(String name, Integer current, Integer size) {
        IPage<HuiLingRobotPointRespVo> ipage = new Page<>(current, size);
        IPage<HuiLingRobotPointRespVo> huiLingRobotPointReqVoIPage = huiLingRobotPointMapper.selectPageList(ipage, name);
        List<HuiLingRobotPointRespVo> records = huiLingRobotPointReqVoIPage.getRecords();
        listCover(records);
        return new GeneratePageListVo<>(huiLingRobotPointReqVoIPage);
    }

    @Override
    public HuiLingRobotPointRespVo getByName(String name) {
        QueryWrapper<HuiLingRobotPointModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HuiLingRobotPointModel.NAME, name);
        HuiLingRobotPointModel huiLingRobotPointModel = huiLingRobotPointMapper.selectOne(queryWrapper);
        if (huiLingRobotPointModel == null) {
            return null;
        }
        return getById(huiLingRobotPointModel.getId());
    }

    public void addOrUpdateCover(HuiLingRobotPointReqVo reqVo, HuiLingRobotPointModel huiLingRobotPointModel) {
        List<String> targetPointVariableNameList = reqVo.getTargetPointVariableNameList();
        List<String> initPointVariableNameList = reqVo.getInitPointVariableNameList();
        huiLingRobotPointModel.setTargetPoint(JSONObject.toJSONString(targetPointVariableNameList));
        huiLingRobotPointModel.setInitPoint(JSONObject.toJSONString(initPointVariableNameList));
    }


    public void listCover(List<HuiLingRobotPointRespVo> records) {
        for (HuiLingRobotPointRespVo record : records) {
            String targetPoint = record.getTargetPoint();
            String initPoint = record.getInitPoint();
            record.setTargetPointVariableNameList(JSONObject.parseArray(targetPoint, String.class));
            record.setInitPointVariableNameList(JSONObject.parseArray(initPoint, String.class));
        }
    }


    private void addOrUpdateCheck(HuiLingRobotPointReqVo reaVo) {
        QueryWrapper<HuiLingRobotPointModel> queryWrapper = new QueryWrapper<>();
        if (reaVo.getId() != null) {
            queryWrapper.ne(HuiLingRobotPointVariableModel.ID, reaVo.getId());
        }
        queryWrapper.eq(HuiLingRobotPointModel.NAME, reaVo.getName());
        Long count = huiLingRobotPointMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("点位名称已经存在！");
        }
    }
}
